package com.example.infowall02.service.impl;
import com.example.infowall02.domain.Articles;
import com.example.infowall02.mapper.ArticlesMapper;
import com.example.infowall02.service.ArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ArticlesServiceImpl implements ArticlesService {

    @Autowired
    private ArticlesMapper articlesMapper;
    //导入redis,泛型为Articles类型
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

//增
    @Override
    public Articles addArticle(Articles article) {
        article.setLikes(0);
        article.setClicks(0);
        article.setFavorites(0);
        int result = articlesMapper.addArticle(article);
        if (result > 0) {
            //将文章存入redis
            redisTemplate.opsForValue().set("article:" + article.getId(), article,1, TimeUnit.HOURS);
            return article;
        }
        return null;
    }

// 删
    @Override
    public boolean deleteArticleById(int id) {
        int rowsAffected = articlesMapper.deleteArticleById(id);
        if (rowsAffected > 0) {
            // 删除缓存
            redisTemplate.delete("article:" + id);
        }
        return rowsAffected > 0;
    }


//查
    @Override
    public List<Articles> findArticlesByKeyword(String keyword, int pageNow, int pageSize) {
        return articlesMapper.findArticlesByKeyword(keyword,pageNow,pageSize);
    }
    @Override
    public int countArticlesByKeyword(String keyword) {
        return articlesMapper.countArticlesByKeyword(keyword);
    }

    @Override
    public int countArticlesByType(String type) {
        return articlesMapper.countArticlesByType(type);
    }

    @Override
    public List<Articles> findArticlesByType(String type, int pageNow, int pageSize) {
        return articlesMapper.findArticlesByType(type,pageNow,pageSize);
    }

    @Override
    public Articles findArticleById(int id) {
        //查找redis中是否有键
        if (Boolean.TRUE.equals(redisTemplate.hasKey("article:" + id))) {
            //如果有，直接返回
            Articles article = (Articles) redisTemplate.opsForValue().get("article:" + id);
            //重置过期时间
            redisTemplate.expire("article:" + id, 1, TimeUnit.HOURS);
            return article;
        }
        //如果没有，从数据库中查找
        Articles article = articlesMapper.findArticleById(id);
        if(article != null) {
            //将文章存入redis
            redisTemplate.opsForValue().set("article:" + id, article,1, TimeUnit.HOURS);
        }
        return article;
    }

    @Override
    public List<Articles> findRecommendArticlesByTime() {
        return articlesMapper.findRecommendArticlesByTime();
    }
    @Override
    public List<Articles> findRecommendArticlesByClicks() {
        return articlesMapper.findRecommendArticlesByClicks();
    }
//改
    @Override
    public int incrementLikes(int id) {
        // 更新缓存
        Articles article = (Articles) redisTemplate.opsForValue().get("article:" + id);
        if (article != null) {
            article.setLikes(article.getLikes() + 1);
            redisTemplate.opsForValue().set("article:" + id, article, 1, TimeUnit.HOURS);
        }
        return articlesMapper.incrementLikes(id);
    }

    @Override
    public int incrementClicks(int id) {
        Articles article = (Articles) redisTemplate.opsForValue().get("article:" + id);
        if (article != null) {
            article.setClicks(article.getClicks() + 1);
            redisTemplate.opsForValue().set("article:" + id, article, 1, TimeUnit.HOURS);
        }
        return articlesMapper.incrementClicks(id);
    }

    @Override
    public int incrementFavorites(int id) {
        Articles article = (Articles) redisTemplate.opsForValue().get("article:" + id);
        if (article != null) {
            article.setFavorites(article.getFavorites() + 1);
            redisTemplate.opsForValue().set("article:" + id, article, 1, TimeUnit.HOURS);
        }
        return articlesMapper.incrementFavorites(id);
    }

    @Override
    public boolean updateArticleInfo(Articles article, int id) {
        int rowsAffected = articlesMapper.updateArticleInfo(article, id);
        if (rowsAffected > 0) {
            // 更新后同步缓存
            Articles updated = articlesMapper.findArticleById(id);
            redisTemplate.opsForValue().set("article:" + id, updated, 1, TimeUnit.HOURS);
        }
        return rowsAffected > 0;
    }
}
